package com.guojian.common.utils;

import java.text.DecimalFormat;
import java.util.regex.Pattern;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

import com.google.common.base.CaseFormat;
import com.guojian.common.format.MyDecimalFormat;

public class StringUtilss extends org.springframework.util.StringUtils {

	private static final DecimalFormat formatInt = new DecimalFormat("#");
	private static final MyDecimalFormat format2Num = new MyDecimalFormat("#.##");
	private static final MyDecimalFormat format2Rate = new MyDecimalFormat("#.##%");

	/**
	 * 增加了对null字符串的判断
	 *
	 * @param value
	 * @return
	 */
	public static boolean isBlank(Object value) {
		if (value == null) {
			return true;
		}

		if (StringUtils.isBlank(ObjectUtils.toString(value))) {
			return true;
		}

		if ("null".equals(ObjectUtils.toString(value).toLowerCase())) {
			return true;
		}

		return false;
	}

	public static boolean isNotBlank(Object value) {
		return !isBlank(value);
	}

	/**
	 * 去除了所有的空格
	 *
	 * @param value
	 * @return
	 */
	public static String toString(Object value) {
		return StringUtils.deleteWhitespace(ObjectUtils.toString(value));
	}

	public static boolean isNumber(Object data) {
		if (StringUtilss.isBlank(data)) {
			return false;
		}
		try {
			Double.parseDouble(data.toString());
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	public static int getIntObject(Object num) {
		if (num == null) {
			return 0;
		} else if (isBlank(num)) {
			return 0;
		} else {
			try {
				return Integer.parseInt(formatInt.format(Double.valueOf(toString(num))));
			} catch (NumberFormatException e) {
				return 0;
			}
		}
	}

	public static double getDoubleObject(Object num) {
		if (num == null) {
			return 0d;
		} else if (StringUtils.isBlank(toString(num))) {
			return 0d;
		} else {
			try {
				return Double.parseDouble(toString(num));
			} catch (NumberFormatException e) {
				return 0d;
			}
		}
	}

	public static double getTimeProcess() {
		return 1.00 / DateUtilss.getNowMonthMaxDay() * DateUtilss.getYesterdayDateNum();
	}

	public static boolean isIntNum(String num) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(num).matches();
	}

	/**
	 * 获取百分比值
	 *
	 * @param value
	 * @param format
	 * @return
	 */
	public static String getFormatRateValue(Object value, MyDecimalFormat format) {
		if (format == null) {
			format = format2Rate;
		}
		return format.format(value);
	}

	public static String getFormatValue2Rate(Object value) {
		return getFormatRateValue(value, null);
	}

	public static String getFormatValue2Num(Object value) {
		return getFormatRateValue(value, format2Num);
	}

	/**
	 * 从百分比中获取实际数值
	 *
	 * @return
	 */
	public static double getDoubleValueOfRate(Object rate) {

		String rate1 = ObjectUtils.toString(rate);

		if (StringUtils.isBlank(rate1)) {
			return 0;
		}

		if (!rate1.contains("%")) {
			return getDoubleObject(rate1);
		}

		return getDoubleObject(rate1.replaceAll("%", "")) / 100;

	}

	public static String[] getArrayOfString(String params, String regex) {
		return tokenizeToStringArray(params, regex);
	}

	public static String[] getArrayOfString(String params) {
		return getArrayOfString(params, ",");
	}

	public static String getTimestamp() {
		return String.valueOf(System.currentTimeMillis());
	}

	/**
	 * 驼峰命名字符转换 TESTDATA ====> testdata TEST_DATA ====> testData test_data ====>
	 * testData
	 *
	 * @param value
	 * @return
	 */
	public static String toLowerCaseOfHump(String value) {
		return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, value);
	}

	public static void main(String[] args) {
		int a = 1001;
		Integer b = 1001;
		System.out.println(a == b);
		System.out.println(formatInt.format(Double.valueOf(12212)));

		// System.out.println(new DecimalFormat("#").format("264"));
		// System.out.println(getFormatValue(-2.6321525885558583, new
		// MyDecimalFormat()));

		System.out.println(getFormatValue2Rate("0.0026"));
		System.out.println(StringUtilss.getDoubleValueOfRate("0.26%"));

		System.out.println("b = " + System.currentTimeMillis());
		System.out.println("getTimestamp() = " + getTimestamp());
		System.out.println("getTimeProcess() = " + getTimeProcess());

		System.out.println("String.join(\",\",\"a\",\"b\") = " + String.join(",", "a", "b"));

	}

}
